<?php
require_once ("../src/db/connectdatabase.php");

function getRowCompareRate($bank, $nhacungcap) {
	$con = new ConnectDB();
	
	$sql = "SELECT COUNT(*) FROM compareloanspersional WHERE bankid LIKE '%".$bank."%' AND isbankphi LIKE '%".$nhacungcap."%' ORDER BY datecreate";
	return $con -> getvalueString($sql);
}

function getCompareRates($bank, $nhacungcap, $start, $lenght) {
	$con = new ConnectDB();
	
	$sql = "SELECT c.idcom, c.bankid, b.bankname, b.banklogo, c.idloanmaxi, c.amountloanmin, c.amountloanmax, c.timeloan, c.interrestrate, c.interrestrate2, c.phitattoan, c.isuse FROM compareloanspersional AS c, banks AS b WHERE c.bankid=b.bankid AND c.bankid LIKE '%".$bank."%' AND c.isbankphi LIKE '%".$nhacungcap."%' ORDER BY c.datecreate DESC LIMIT ".$start.",".$lenght;
	return $con -> getvalueString($sql);
}

$bank = "";
$mucdich = "";
$nhacungcap = "";

if(isset($_GET['bank'])) {
	$bank = $_GET['bank'];
}

if(isset($_GET['cp'])) {
	$nhacungcap = $_GET['cp'];
}

$page = 1;
if(isset($_GET['page'])) {
	$page = $_GET['page'];
}

$arrcountrows = getRowCompareRate($bank, $nhacungcap);

$sumrecord = $arrcountrows[0][0];
$numrecord = 50;
$pageNumber = intval(($sumrecord / $numrecord));
if(($sumrecord % $numrecord) != 0) {
	$pageNumber++;
}

if($pageNumber == 0) {
	$pageNumber = 1;	
}

if($page > $pageNumber) {
	$page = $pageNumber;
}

$arrdata = getCompareRates($bank, $nhacungcap, ($page-1)*$numrecord, $numrecord);
?>
<style>
.listviewcontent table {
	width: 900px;
	max-width: 900px;
}

.listviewcontent table td {
	margin-top: 2px;
	border: 1px solid;
	border-radius: 2px 2px 2px 2px;
}

.title {
	text-align: center;
	background-color: #0088cc;
	min-height: 40px;
	-moz-border-radius: 8px 8px 0px 0px;
	-webkit-border-radius: 8px 8px 0px 0px;
	border-radius: 8px 8px 0px 0px;
	color: #FFF;
}

.listviewcontent table tr:hover td {
	background: #FFC;
}
</style>
<h3>Danh Sách So Sánh Cho Vay</h3>
<div>
	<div style="width:600px; float:left;">
    	<table>
            <tr>
                <td>TK Theo Ngân Hàng:</td>
                <td>TK Nhà Cung Cấp:</td>
                <td></td>
            </tr>
            <tr>
                <td><select name="selectbank" class="bankloadselect" style="width: 150px; height: 30px; margin:5px; padding:5px;"><option value="">--- Tất Cả ---</option></select></td>
                <td><select class="class-inputselect-search-left class-mucdichvay-search" id="form_search_bank" style="width: 150px; height: 30px; margin:5px; padding:5px;"><option value="">-- Tất Cả --</option><option value="bank">Ngân Hàng</option><option value="phibank">Phi Ngân Hàng</option></select></td>
                <td><input id="form_submitsearch" type="button" style="cursor:pointer ;background-image:url(../images/timkiem.png); width:70px; height:24px; border:0px;" value="" /></td>
            </tr>
        </table>
    </div>
    <div style="clear:both; height:15px;"></div>
    <div style="float:left; width: 300px;"><a href="admin.php?content=quanlivay&&as=add-compare-rate"><img src="../images/icon-add.jpg" title="Thêm So Sánh Vay" />Thêm So Sánh Vay</a></div>
    <div style="float:right; width: 600px; text-align: right;">
        <strong>Chú Thích:</strong><img src="../images/unclockicon.png" title="Được Phép Hiển Thị" /> Hiển Thị &nbsp;&nbsp;<img src="../images/lockicon.png" title="Ẩn" /> Ẩn
    </div>
    <div style="clear:both;"></div>
    <div class="class-pagination">
        <span class="class-pageLabel">Trang</span>
        <?php if($page == 1) { ?>
        <span title="Trang 1" page="1" class="class-pageNow">1</span>
        <?php } else { ?>
        <span title="Trang 1" page="1" class="class-pageElem">1</span>
        <?php }?>
        <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
        <?php if($i == $page) { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
        <?php } else { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
        <?php }?>
        <?php }}?>
    </div>
    <div style="clear:both; height:15px;"></div>
    <div class="listviewcontent">
    	<table>
        	<tr>
            	<th class="title" width="40px">STT</th>
            	<th class="title" width="80px">Ngân Hàng</th>
                <th class="title" width="130px">Khoản vay tối đa</th>
                <th class="title" width="140px">Số Tiền Vay</th>
                <th class="title" width="110px">Lãi Suất</th>
                <th class="title" width="120px">Phí tất toán</th>
                <th class="title" width="80px">Thời Gian</th>
                <th class="title" width="90px">Chức Năng</th>
            </tr>
            <?php
			$i = ($page-1)*$numrecord + 1;
			foreach($arrdata as $entry) {
			?>
            <tr>
            	<td align="center"><?php echo($i++); ?></td>
            	<td align="center"><img src="../<?php echo($entry[3]) ?>" width="80px" alt="<?php echo($entry[2]) ?>(<?php echo($entry[1]) ?>)" title="<?php echo($entry[2]) ?>(<?php echo($entry[1]) ?>)" height="25px" style="margin: 5px;" /></td>
                <td align="center"><?php $arridloanmaxi = explode(',', $entry[4]); for($j = 0; $j < count($arridloanmaxi); $j++) { if($arridloanmaxi[$j] == '1') echo('Dưới 6 lần thu nhập. '); else echo('Trên 6 lần thu nhập. '); } ?></td>
                <td align="center"><?php echo(changnumbermoney($entry[5])) ?> - <?php echo(changnumbermoney($entry[6])) ?></td>
                <td align="center" style="font-size:11px"><?php echo($entry[8]) ?>% - <?php echo($entry[9]) ?>%</td>
                <td align="center"><?php echo($entry[10]) ?></td>
                <td align="center"><?php echo($entry[7]) ?> năm</td>
                <td align="center"><?php if($entry[11] == "1") { ?><img class="class-display-link" idpr="<?php echo($entry[0]) ?>" style="cursor:pointer;" src="../images/unclockicon.png" title="Click để không cho phép hiển thị" /><?php } else { ?><img class="class-hiden-link" idpr="<?php echo($entry[0]) ?>" style="cursor:pointer;" src="../images/lockicon.png" title="Click để hiển thị trên trang chủ" /><?php } ?>  <a href="admin.php?content=quanlivay&&as=edit-compare-rate&&id=<?php echo($entry[0]) ?>"><img src="../images/icon_detail_cardiscount.png" title="Cập Nhật Thông Tin" /></a></td>
            </tr>
            <?php 
			} ?>
		</table>
    </div>
    <div style="clear:both;"></div>
    <div class="class-pagination">
        <span class="class-pageLabel">Trang</span>
        <?php if($page == 1) { ?>
        <span title="Trang 1" page="1" class="class-pageNow">1</span>
        <?php } else { ?>
        <span title="Trang 1" page="1" class="class-pageElem">1</span>
        <?php }?>
        <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
        <?php if($i == $page) { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
        <?php } else { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
        <?php }?>
        <?php }}?>
    </div>
    <div style="clear:both; height:15px;"></div>
    <div style="float:left; width: 300px;"><a href="admin.php?content=quanlivay&&as=add-compare-rate"><img src="../images/icon-add.jpg" title="Thêm So Sánh Vay" />Thêm So Sánh Vay</a></div>
    <div style="clear:both;"></div>
</div>

<script>

$(document).on('click', '.class-display-link', function() {
	elem = $(this);
	id = elem.attr('idpr');
	$.ajax({
		type: "POST",
		url: "../src/ajax/ajaxadmincompare.php",
		data:{'act': 'edit', 'type': '0', 'idpe': id},
		dataType: "json",
		success: function(response){
			if(response == true) {
				alert("Cập nhật thành công");
				elem.parent().html('<img class="class-hiden-link" idpr="' + id + '" style="cursor:pointer;" src="../images/lockicon.png" title="Click để hiển thị trên trang chủ" />  <a href="admin.php?content=quanlivay&&as=edit-compare-rate&&id=' + id + '"><img src="../images/icon_detail_cardiscount.png" title="Cập Nhật Thông Tin" /></a>');
			} else {
				alert("Cập nhật thất bại");
			}
		}
	});
});

$(document).on('click', '.class-hiden-link', function() {
	elem = $(this);
	id = elem.attr('idpr');
	$.ajax({
		type: "POST",
		url: "../src/ajax/ajaxadmincompare.php",
		data:{'act': 'edit', 'type': "1", 'idpe': id},
		dataType: "json",
		success: function(response){
			if(response == true) {
				alert("Cập nhật thành công");
				elem.parent().html('<img class="class-display-link" idpr="' + id + '" style="cursor:pointer;" src="../images/unclockicon.png" title="Click để không cho phép hiển thị" />  <a href="admin.php?content=quanlivay&&as=edit-compare-rate&&id=' + id + '"><img src="../images/icon_detail_cardiscount.png" title="Cập Nhật Thông Tin" /></a>');
			} else {
				alert("Cập nhật thất bại");
			}
		}
	});
});

$(document).on('click', '.class-pageElem', function() {
	page = $(this).attr('page');
	var bank = $('.bankloadselect').val();
	var md = $('#form_search_mucdich').val();
	var cp = $('#form_search_bank').val();
	window.location = 'admin.php?content=quanlivay&&as=qly-so-sanh-vay&bank=' + bank + '&md=' + md + '&cp=' + cp + '&page=' + page;
});

$(document).ready(function(e) {
	$("#form_search_bank option[value='<?php echo($nhacungcap) ?>']").attr('selected', 'selected');
    loadbank();
	function loadbank() {
		$.ajax({
			url: "../src/ajax/ajaxbank.php",
			type: "post",
			data: {act:'getbanks'},
			dataType:"json",
			success: function(response) {
				html = '';
				$.each(response, function(entryIndex, entry){
					html += '<option value="' + entry[0] + '">' + entry[1] + ' (' + entry[0] + ')</option>';
				});
				$('.bankloadselect').append(html);
				$(".bankloadselect option[value='<?php echo($bank) ?>']").attr('selected', 'selected');
			}
		});
	}
	
	$('#form_submitsearch').click(function(e) {
		var bank = $('.bankloadselect').val();
		var md = $('#form_search_mucdich').val();
		var cp = $('#form_search_bank').val();
       	openwindowns('admin.php?content=quanlivay&&as=qly-so-sanh-vay&bank=' + bank + '&md=' + md + '&cp=' + cp);
    });
	
	function openwindowns(url) {
		window.location = url;
	}
	
});
</script>